﻿using System;
using System.Collections.Generic;
using System.Data;
using Framework;

namespace Enterprise.Admin.Handle
{
    public partial class GetColumnTreeDataJson : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var columnService = new ColumnService();
            var table = columnService.GetTable();
            var list = new List<object>();
            foreach (DataRow item in table.Rows)
            {
                if (item["CoParentId"].SafeToInt() == 0)
                {
                    list.Add(new
                    {
                        id = item["CoColumnId"].SafeToInt(),
                        text = item["CoName"].SafeToString(),
                        children = TreeCreate(item["CoColumnId"].SafeToInt(), table.Rows)
                    });
                }
            }
            Json(list);
        }
        private List<object> TreeCreate(int id, DataRowCollection dataRows)
        {
            var list = new List<object>();
            foreach (DataRow item in dataRows)
            {
                if (item["CoParentId"].SafeToInt() == id)
                {
                    list.Add(new
                    {
                        id = item["CoColumnId"].SafeToInt(),
                        text = item["CoName"].SafeToString(),
                        children = TreeCreate(item["CoColumnId"].SafeToInt(), dataRows)
                    });
                }
            }
            return list;
        }
    }
}